package com.example.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.demo.dto.UserRequestDTO;
import com.example.demo.dto.UserViewRequestDTO;
import com.example.demo.controller.bean.Result;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.UserManageService;
import com.example.demo.utils.MD5Util;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("userManage")
@Api(value = "用戶信息管理", tags = {"用戶信息管理"})
@RestController
@Slf4j
public class UserManageController {
    @Autowired
    private UserManageService service;

    @ResponseBody
    @ApiOperation(value = "获取用戶列表接口")
    @RequestMapping(value = "getUserList", method = {RequestMethod.POST})
    public Result<PageVo<SysUserView>> getList(@RequestBody PageBo pageBo) {
        PageVo<SysUserView> list = service.getUserList(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation(value = "搜索日志列表接口")
    @RequestMapping(value = "search", method = {RequestMethod.POST})
    public Result<PageVo<SysUserView>> getListSearch(@RequestBody UserRequestDTO request) {
        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        PageVo<SysUserView> logs = service.searchUser(request, pageBo);
        return Result.ok(logs);

    }

    @ResponseBody
    @ApiOperation("删除用戶接口")
    @RequestMapping(value = "delete", method = {RequestMethod.POST})
    public Result<String> delete(@RequestBody SysUserView user) {
        service.deleteUser(user);
        return Result.ok("删除成功");
    }

    @ResponseBody
    @ApiOperation("批量删除接口")
    @RequestMapping(value ="deleteList", method = {RequestMethod.POST})
    public Result<String> deleteList(@RequestBody List<SysUserView> userList) {
        for (SysUserView user : userList) {
            System.out.println(user);
        }
        service.deleteList(userList);
        return Result.ok("删除成功");
    }

    @ResponseBody
    @ApiOperation("修改用戶接口")
    @RequestMapping(value = "update", method = {RequestMethod.POST})
    public Result<String> updateUserInfo(@RequestBody SysUserView user) {
        return service.updateUser(user);
    }


    @ResponseBody
    @RequestMapping(value = "add", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation("用户新增接口")
    public Result<String> addUserInfo(@RequestBody SysUserView user) {
        return service.addUserInfo(user);
    }

    @ResponseBody
    @ApiOperation(value = "获取广告主列表接口")
    @RequestMapping(value = "getAdvertiserList", method = {RequestMethod.POST})
    public Result<PageVo<AdvertiserView>> getAdvertiserList(@RequestBody PageBo pageBo) {
        PageVo<AdvertiserView> list = service.getAdvertiserList(pageBo);
        return Result.ok(list);
    }

    @ResponseBody
    @ApiOperation(value = "搜索广告主接口")
    @RequestMapping(value = "searchAdvertiser", method = {RequestMethod.POST})
    public Result<PageVo<AdvertiserView>> searchAdvertiser(@RequestBody UserRequestDTO request) {
        PageBo pageBo = new PageBo();
        pageBo.setPageNum(request.getPageNum());
        pageBo.setPageSize(request.getPageSize());

        PageVo<AdvertiserView> logs = service.searchAdvertiser(request, pageBo);
        return Result.ok(logs);

    }

    @ResponseBody
    @ApiOperation("广告主詳情显示接口")
    @RequestMapping(value = "advertiserDetails/{userId}")
    public Result<List<AdvertiserView>> getAdvertiserDataList(@PathVariable("userId")int userId){
        System.out.println("調用訂單詳情显示接口");
        List<AdvertiserView> list = service.getAdvertiserDataList(userId);
        return Result.ok(list);
    }

    @ResponseBody
    @RequestMapping(value = "addAdvertiser", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation("广告主新增接口")
    public Result<String> addAdvertiser(@RequestBody AdvertiserView request) {
        return service.addAdvertiser(request);
    }

    @ResponseBody
    @ApiOperation("删除广告主接口")
    @RequestMapping(value = "deleteAdvertiser", method = {RequestMethod.POST})
    public Result<String> deleteAdvertiser(@RequestBody AdvertiserView request) {
        return service.deleteAdvertiser(request);
    }

    @ResponseBody
    @ApiOperation("批量删除广告主接口")
    @RequestMapping(value ="batchDeleteAdvertiser", method = {RequestMethod.POST})
    public Result<String> batchDeleteAdvertiser(@RequestBody List<AdvertiserView> advertiserList) {
        return service.batchDeleteAdvertiser(advertiserList);
    }

    @ResponseBody
    @ApiOperation("修改广告主接口")
    @RequestMapping(value = "updateAdvertiser", method = {RequestMethod.POST})
    public Result<String> updateAdvertiser(@RequestBody AdvertiserView request) {
        return service.updateAdvertiser(request);
    }
}
